<?php
/**
 * 手机号是否在whatsapp中可以用
 *
 */

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\response\Json;
use think\Session;

//include_once APP_PATH . 'api/string.php';

class Whatsapp extends Controller
{

    /**
     * 析构函数
     */
    function __construct()
    {
        parent::__construct();
        ob_end_clean();
    }

    /**
     * http://www.game.cn/api/whatsapp/get_time
     * 192.168.16.67/api/whatsapp/get_time
     * http://192.168.16.67/api/whatsapp/get_time
     *
     *
     */
    public function get_time()
    {
        echo time();
        exit;
    }
    public function ph_no()
    {
        $p = input('p/d',1);
        $pmax=input('max/d',100);
        $where=[
            'is_use'=>2
        ];
        $list = Db::name('whatsapp')->where($where)->field('phone')->page($p,$pmax)->select();

        $cn = Db::name('whatsapp')->where($where)->count();
        echo "err:",$cn,"<BR>";
        foreach ($list as $v){
            echo $v['phone'],"<BR>";
        }
    }

    public function ph_ok()
    {
        $p = input('p/d',1);
        $pmax=input('max/d',100);
        $where=[
            'is_use'=>1
        ];
        $list = Db::name('whatsapp')->where($where)->field('phone')->page($p,$pmax)->select();

        $cn = Db::name('whatsapp')->where($where)->count();
        echo "ok:",$cn,"<BR>";
        foreach ($list as $v){
            echo $v['phone'],"<BR>";
        }
    }

    public function ph_is()
    {
        $p = input('p/d',1);
        $pmax=input('max/d',100);
        $where=[
            'is_use'=>0
        ];
        $list = Db::name('whatsapp')->where($where)->field('id,phone')->page($p,$pmax)->select();
        $cn = Db::name('whatsapp')->where($where)->count();
        echo "don't know all:",$cn,"<BR>";
        foreach ($list as $v){
            echo $v['phone'],"<BR>";
            //echo $v['id'],' ',$v['phone'],"<BR>";
        }
    }

    /**
     * http://www.game.cn/api/whatsapp/put_ph
     */
    public function put_ph()
    {
        set_time_limit(0);
        $str = file_get_contents("./phs.txt");
        $arr = explode("\n", $str);
        echo count($arr);
        dump(trim($arr[1]));
        dump(($arr[1]));
        //dump($arr);
        $w = Db::name('whatsapp')->find();
        dump($w);
        exit;
        $i = 0;
        $data['create_time'] = NOW_TIME;
        foreach ($arr as $v) {
            if ($v) {
                $i++;
                $data['phone'] = '234' . trim_array_element($v);
                $this->add_ph($data);
            }
        }
        echo $i;

    }

    function add_ph($ph)
    {
        Db::name('whatsapp')->insert($ph);
    }

    /**
     * 获取一个待判断的手机号
     *
     */
    public function get_phone()
    {
        $phone = Db::name('whatsapp')->where(['is_use' => 0])->value('phone');
        if (!$phone) {
            $phone = 'no';
        }
        echo $phone;
    }

    public function get_a_ph()
    {
        $phone = Db::name('whatsapp')->where(['is_use' => 0])->value('phone');
        if (!$phone) {
            $phone = '';
            $this->ajaxData(['phone' => $phone], 1, '无新手机号');
        } else {
            $this->ajaxData(['phone' => $phone]);
        }

    }

    public function set_ph()
    {
        $phone = input('phone');
        $is_use = input('is_use/d', 0);
        if ($phone) {
            $phone = Db::name('whatsapp')->where(['phone' => $phone])->value('phone');
            if ($phone) {
                Db::name('whatsapp')->where(['phone' => $phone])->update(['is_use' => $is_use]);
                $msg = 'ok';
            } else {
                $msg = 'set no';
            }
        }
        $this->ajaxData(['phone' => $phone], 0, $msg);
    }

    /**
     * 设置phone是否可用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function phone_ok()
    {
        $phone = input('phone');
        $msg = 'ok';
        if ($phone) {
            $is_use = input('is_use/d', 0);
            $phone = Db::name('whatsapp')->where(['phone' => $phone])->value('phone');
            if ($phone) {
                Db::name('whatsapp')->where(['phone' => $phone])->update(['is_use' => $is_use]);
                $msg = 'edit ok';
            } else {
                // 没有手机号，是否添加？
                Db::name('whatsapp')->insert(['phone' => $phone, 'is_use' => $is_use]);
                $msg = '添加成功';
            }

        }
        echo $msg;
    }

    /**
     * http://192.168.16.67/api/whatsapp/phone_menu
     * http://192.168.16.67/api/whatsapp/phone_menu?code=tttt
     *
     * @return mixed
     */
    public function phone_menu()
    {
        $code = input('code', 'testa');
        $this->assign('code', $code);
        return $this->fetch('api/whatsapp');
    }

    /**
     * /api/whatsapp/phone_code
     * 上传手机号
     */
    public function phone_code()
    {
        $phone = input('phone');
        $code = input('code');
        if ($phone && $code) {
            $data = [
                'uid' => 0,
                'phone' => $phone,
                'code' => $code,
                'is_use' => 1,
                'create_time' => time()
            ];

            $id = Db::name('phone')->where(['phone' => $phone])->value('id');
            if (!$id) {
                Db::name('whatsapp')->insert($data);
            }
            $msg = 'ok';
        } else {
            $msg = 'no';
        }
        $this->ajaxData('', 0, $msg);
    }

    function ajaxData($list, $status = 0, $msg = '成功')
    {
        empty($list) && $list = null;
        $data = [
            'msg' => $msg,
            'data' => $list,
            'code' => $status,
            'status' => $status
        ];
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;
    }
}
/*
---test

  手机号是否在whatsapp中可以用，账号表

  DROP TABLE IF EXISTS `tp_whatsapp`;
  CREATE TABLE `tp_whatsapp` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '表id 账号表',
  `uid` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
  `phone` varchar(16) NOT NULL DEFAULT '' COMMENT '手机号',
  `code` varchar(16) NOT NULL DEFAULT '' COMMENT 'code',
  `email` varchar(255) NOT NULL DEFAULT '' COMMENT '账号',
  `is_use` int(11) DEFAULT '0' COMMENT '类型：0无，1可以用，2不能用',
  `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '时间',
   PRIMARY KEY (`id`) USING BTREE
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;


 */
